package com.aloda.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.ErrorCode;
import com.aloda.dto.analysis.AnalysisDTO;
import com.aloda.exception.BaseException;
import com.aloda.exception.WarnException;
import com.aloda.result.Result;
import com.aloda.service.AnalysisService;
import com.aloda.vo.analysis.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 数据统计页面统一controller
 * @author aloda
 * @date 2024/12/28
 */
@Slf4j
@RequestMapping("/admin/analysis")
@RestController("ana-admin")
public class AnalysisController {

    @Resource
    private AnalysisService analysisService;

    /**
     * 分析用户数据
     * @param analysisDTO
     * @return
     */
    @PostMapping("/user")
    public Result<AnalysisUserVO> analysisUserData(@RequestBody AnalysisDTO analysisDTO) {
        if (BeanUtil.isEmpty(analysisDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        AnalysisUserVO analysisUserVO = analysisService.analysisUserData(analysisDTO);
        return Result.success(analysisUserVO);
    }

    /**
     * 分析应用数据
     * @param analysisDTO
     * @return
     */
    @PostMapping("/app")
    public Result<AnalysisAppVO> analysisAppData(@RequestBody AnalysisDTO analysisDTO) {
        if (BeanUtil.isEmpty(analysisDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        AnalysisAppVO analysisAppVO = analysisService.analysisAppData(analysisDTO);
        return Result.success(analysisAppVO);
    }

    /**
     * 分析登录数据
     * @param analysisDTO
     * @return
     */
    @PostMapping("/login")
    public Result<AnalysisLoginVO> analysisLoginData(@RequestBody AnalysisDTO analysisDTO) {
        if (BeanUtil.isEmpty(analysisDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        AnalysisLoginVO analysisLoginVO = analysisService.analysisLoginData(analysisDTO);
        return Result.success(analysisLoginVO);
    }

    /**
     * 分析图片资源
     * @param analysisDTO
     * @return
     */
    @PostMapping("/pic")
    public Result<AnalysisPicResourceVO> analysisPicResource(@RequestBody AnalysisDTO analysisDTO) {
        if (BeanUtil.isEmpty(analysisDTO)) {
            throw new WarnException(ErrorCode.PARAMS_ERROR);
        }
        AnalysisPicResourceVO analysisPicResourceVO = analysisService.analysisPicResource(analysisDTO);
        return Result.success(analysisPicResourceVO);
    }

    /**
     * 获取在线用户数量
     * @return
     */
    @GetMapping("/online")
    public Result analysisOnlineUser() {
        return Result.success(analysisService.analysisOnlineUser());
    }

    /**
     * 获取pv总数量
     * @return
     */
    @GetMapping("/pv")
    public Result getPV() {
        long result = analysisService.getPV();
        return Result.success(result);
    }

    /**
     * 获取UV总数量
     * @return
     */
    @GetMapping("/uv")
    public Result getUV() {
        long result = analysisService.getUV();
        return Result.success(result);
    }

    /**
     * 分析图片资源容量
     * @return
     */
    @GetMapping("/pic")
    public Result<AnalysisPicVO> analysisPicCapacity() {
        return Result.success(analysisService.analysisPicCapacity());
    }
}
